package com.see.network.impl;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

class HtmlReader {

    public String readHtml(String urlString, boolean proxyEnable) {
        @SuppressWarnings("unused")
        ProxyManager proxyManager = new ProxyManager(proxyEnable);
        StringBuffer document = new StringBuffer();
        // initialize connection
        HttpURLConnection conn = null;
        InputStream is = null;
        DataInputStream isr = null;
        try {
            URL url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            //conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            is = conn.getInputStream();
            isr = new DataInputStream(is);
            int line;
            while ((line = isr.read()) != -1)
                document.append((char) line);

            String htmlContent = replaceHtmlEntities(document.toString());//document.toString()

            if (htmlContent == null || htmlContent.trim().length() == 0) {
                System.err.println("Cannot find any html contents!!");
            }
            return htmlContent;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                isr.close();
                isr.close();
                conn.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private String replaceHtmlEntities(String string) {

        if (string.contains("&amp;")) {
            string = string.replaceAll("&amp;", "&");
        }

        return string;
    }

}
